{% extends 'layout.html' %}

{% block content %}
    <div class="container">
        <div style="margin-bottom: 10px">
            <input id="btnAdd" type="button" value="新建订单" class="btn btn-success">
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">
                <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
                订单列表
            </div>
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>订单号</th>
                        <th>名称</th>
                        <th>价格</th>
                        <th>状态</th>
                        <th>管理员</th>
                        <th>操作</th>  
                    </tr>
                </thead>
                <tbody>
                    {% for obj in queryset %}
                        <tr uid="{{ obj.id }}">
                            <th>{{ obj.id }}</th>
                            <td>{{ obj.oid }}</td>
                            <td>{{ obj.title }}</td>  
                            <td>{{ obj.price }}</td>
                            <td>{{ obj.get_status_display }}</td>
                            <td>{{ obj.admin.username }}</td>
                            <td>
                                <a class="btn btn-primary btn-xs" href="/admin/{{ obj.id }}/edit/">编辑</a>
                                <input uid="{{ obj.id }}" class="btn btn-danger btn-xs btn-delete" type="button" value="删除">
                            </td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <ul class="pagination">
            {{ page_string }}
        </ul>

        <!-- 新建订单对话框 -->
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">新建订单</h4>
                    </div>
                    <div class="modal-body">
                        <form id="formAdd">
                            <div class="clearfix">
                                {% for field in form %}
                                    <div class="col-xs-6">
                                        <div class="form-group" style="position: relative;margin-bottom: 20px;">
                                            <label>{{ field.label }}</label>
                                            {{ field }}
                                            <span class="error-msg" style="color: red;position:absolute;"></span>
                                        </div>
                                    </div>
                                {% endfor %}
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button id="btnSave" type="button" class="btn btn-primary">保存</button>
                    </div>
                </div>
            </div>
        </div>

        <!-- 删除订单对话框 -->
        <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog" role="document">
                <div class="alert alert-danger alert-dismissible fade in" role="alert">
                    <h4>是否确定删除？</h4>
                    <p style="margin : 10px 0;">删除后所有相关的关联数据将会被删除！</p>
                    <p style="text-align: right">
                        <button id="btnConfirmDelete" type="button" class="btn btn-danger">确定</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    </p>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block js %}
    <script type="text/javascript">
        var DELETE_ID;

        $(function () {
            bindBtnAddEvent();
            bindBtnSaveEvent();
            bindBtnDeleteEvent();
            bindBtnConfirmDeleteEvent();
        });

        function bindBtnAddEvent() {
            $("#btnAdd").click(function () {
                // 点击新建按钮， 显示对话框
                $('#myModal').modal('show');
            });
        }

        function bindBtnSaveEvent() {
            $("#btnSave").click(function () {
                // 清除之前的错误信息
                $(".error-msg").empty();

                // 转成 AJAX 请求
                $.ajax({
                    url: "/order/add", 
                    type: "POST",    
                    data: $("#formAdd").serialize(),
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            // 清空表单
                            $("#formAdd")[0].reset();  
                            // 关闭对话框
                            $('#myModal').modal('hide');
                            // 刷新页面
                            location.reload();
                        } else {
                            $.each(res.error, function (name, errorList) {
                                $("#id_" + name).next().text(errorList[0]);
                            });
                        }
                    }
                });
            });
        }

        function bindBtnDeleteEvent() {
            $(".btn-delete").click(function () {
                // 显示删除对话框
                $("#deleteModal").modal('show');
                // 获取当前行的ID赋值给全局变量
                DELETE_ID = $(this).attr("uid");
            });
        }

        function bindBtnConfirmDeleteEvent() {
            $("#btnConfirmDelete").click(function () {
                $.ajax({
                    url: "/order/delete/",  //    => /order/delete/?uid=123
                    type: "GET",
                    data: {
                        uid: DELETE_ID
                    },
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            location.reload();
                        } else {
                            // 删除失败
                            alert(res.error);
                        }
                    }
                })
            });
        }
    </script>
{% endblock %}
